<?php

namespace App\Http\Controllers;

use App\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MessageController extends Controller
{
    /**
     * 消息列表 (用于首页)
     *
     * @param  Request
     * @return Response
     */
    public function listMessage(Request $request)
    {
        try{
            $userId = $request->input('userId', '');
            if(empty($userId)){
                return response()->json(['success'=>false,'code'=>3001,'message'=>'必填参数不能为空']);
            }
            $where[] = ['user_message.is_del','=','2'];
            $where[] = ['user_message.status','=','1'];
            $where[] = ['user_message.busers_id','=',$userId];
            $message = $request->input('message', '');
            if(!empty($message)){
                $where[] = ['user_message.message','like','%'.$message.'%'];
            }
            //当前页码
            $pageNumber = $request->input('pageNumber', 1);
            //每页显示数量
            $pageSize = $request->input('pageSize', 10);
            $pyl = ($pageNumber-1)*$pageSize;
            $res = DB::table('user_message')->select('user_message.message','user_message.create_time','users.head_pic','users.nick_name','user_message.goods_id')
                ->join('users', 'user_message.users_id', '=', 'users.id')
                ->where($where)->orderBy('user_message.id', 'desc')->skip($pyl)->take($pageSize)->get();
            return response()->json(['success'=>true,'code'=>1000,'list'=>$res]);
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }

    /**
     * 获取未读的消息count
     *
     * @param  Request
     * @return Response
     */
    public function countMessage(Request $request)
    {
        try{
            $userId = $request->input('userId', '');
            if(empty($userId)){
                return response()->json(['success'=>false,'code'=>3002,'message'=>'必填参数不能为空']);
            }
            $where = [['user_message.busers_id','=',$userId],['user_message.is_del','=','2'],['user_message.status','=','1'],['user_message.is_read','=','2']];
            $count = DB::table('user_message')->where($where)->count();
            $res['count'] = $count;
            $res['message'] = '';
            $res['time'] = '';
            $res['pic'] = DB::table('user_message')
                ->join('users', 'user_message.users_id', '=', 'users.id')
                ->where($where)->orderBy('user_message.id','desc')->value('users.head_pic');
            if($count > 0){
                $last = DB::table('user_message')->where($where)->orderBy('user_message.id','desc')->first();
                $res['message'] = $last->message;
                $res['time'] = $last->create_time;
            }
            return response()->json(['success'=>true,'code'=>1000,'message'=>$res]);
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }

    /**
     * 删除评论
     *
     * @param  Request
     * @return Response
     */
    public function delMessage(Request $request)
    {
        try{
            $userId = $request->input('userId', '');
            $messageId = $request->input('messageId', '');
            if(empty($userId) || empty($messageId)){
                return response()->json(['success'=>false,'code'=>3003,'message'=>'必填参数不能为空']);
            }
            $data['is_del'] = 1;
            $data['del_time'] = time();
            $update = DB::table('user_message')->where([
                ['id','=',$messageId],
                ['users_id','=',$userId]
            ])->update($data);
            if($update){
                return response()->json(['success'=>true,'code'=>1000,'message'=>'删除成功']);
            }else{
                return response()->json(['success'=>false,'code'=>3004,'message'=>'删除失败']);
            }
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }


    /**
     * 标记已读消息
     *
     * @param  Request
     * @return Response
     */
    public function readMessage(Request $request)
    {
        try{
            $userId = $request->input('userId', '');
            if(empty($userId)){
                return response()->json(['success'=>false,'code'=>3005,'message'=>'必填参数不能为空']);
            }
            $data['is_read'] = 1;
            $update = DB::table('user_message')->where([
                ['busers_id','=',$userId]
            ])->update($data);
            if($update !== false){
                return response()->json(['success'=>true,'code'=>1000,'message'=>'标记已读成功']);
            }else{
                return response()->json(['success'=>false,'code'=>3006,'message'=>'标记已读失败']);
            }
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }
}
